home *** CD-ROM | disk | FTP | other *** search
/ Pocket PC Game Programming / Pocket PC Game Programming.iso / source.exe / CH15 / GameLibrary / CWaveOut.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  3.0 KB  |  138 lines

  1. ////////////////////////////////////////////////////////////
  2. // Pocket PC Game Programming
  3. // Chapter 10: Sound Effects and Music
  4. //
  5. // CWaveOut Source File
  6. //
  7. // This file includes the CWaveOut definition.
  8. // Only supports uncompressed Windows PCM wave format.
  9. // 
  10. ////////////////////////////////////////////////////////////
  11.  
  12. #include "stdafx.h"
  13. #include "CWaveOut.h"
  14.  
  15. ////////////////////////////////////////////////////////////
  16. // CWaveOut Constructor
  17. //
  18. // This function is called when the class is instantiated.
  19. //
  20. CWaveOut::CWaveOut()
  21. {
  22.     dwBytesRead = 0;
  23.     lpWave = NULL;
  24.     bRepeat = FALSE;
  25. }
  26.  
  27. ////////////////////////////////////////////////////////////
  28. // CWaveOut Constructor (Overloaded)
  29. //
  30. // This function is called when the class is instantiated.
  31. // Loads the wave passed as a parameter.
  32. //
  33. CWaveOut::CWaveOut(LPTSTR lpFile)
  34. {
  35.     dwBytesRead = 0;
  36.     lpWave = NULL;
  37.     if (wcslen(lpFile) > 0)
  38.         Load(lpFile);
  39. }
  40.  
  41. ////////////////////////////////////////////////////////////
  42. // CWaveOut Destructor
  43. //
  44. // This function is called when the class is terminated.
  45. //
  46. CWaveOut::~CWaveOut()
  47. {
  48.     if (lpWave != NULL)
  49.     {
  50.         Stop();
  51.         free((void *)lpWave);
  52.     }
  53. }
  54.  
  55. ////////////////////////////////////////////////////////////
  56. // CWaveOut::Play
  57. //
  58. // Plays the wave buffer from memory (if loaded).
  59. //
  60. void CWaveOut::Play()
  61. {
  62.     if (lpWave != NULL)
  63.     {
  64.         if (bRepeat)
  65.             sndPlaySound(lpWave, SND_MEMORY | SND_ASYNC | SND_LOOP);
  66.         else
  67.             sndPlaySound(lpWave, SND_MEMORY | SND_ASYNC);
  68.  
  69.     }
  70. }
  71.  
  72. ////////////////////////////////////////////////////////////
  73. // CWaveOut::Stop
  74. //
  75. // Stops playback of the wave buffer.
  76. //
  77. void CWaveOut::Stop()
  78. {
  79.     sndPlaySound(NULL, 0);
  80. }
  81.  
  82. ////////////////////////////////////////////////////////////
  83. // CWaveOut::Load
  84. //
  85. // Loads an uncompressed PCM wave file from disk.
  86. //
  87. BOOL CWaveOut::Load(LPTSTR lpFile)
  88. {
  89.     HANDLE hFile;
  90.     DWORD dwSize;
  91.     BOOL bRet;
  92.  
  93.     //first make sure a wave is not already loaded
  94.     if (lpWave != NULL)
  95.     {
  96.         Stop();
  97.         free((void *)lpWave);
  98.     }
  99.  
  100.     //open the file
  101.     hFile = CreateFile(lpFile, GENERIC_READ, FILE_SHARE_READ, NULL,
  102.         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  103.     if (hFile == INVALID_HANDLE_VALUE)
  104.     {
  105.         return FALSE;
  106.     }
  107.  
  108.     //determine wave size in bytes
  109.     dwSize = GetFileSize(hFile, NULL);
  110.     if (dwSize == 0xFFFFFFFF)
  111.     {
  112.         CloseHandle(hFile);
  113.         return FALSE;
  114.     }
  115.  
  116.     //allocate memory for the wave
  117.     lpWave = (LPCTSTR)malloc(dwSize);
  118.     if (lpWave == NULL)
  119.     {
  120.         CloseHandle(hFile);
  121.         return FALSE;
  122.     }
  123.  
  124.     //read the wave file
  125.     bRet = ReadFile(hFile, (void *)lpWave, dwSize, &dwBytesRead, NULL);
  126.     if ((bRet == FALSE) || (dwBytesRead != dwSize))
  127.     {
  128.         CloseHandle(hFile);
  129.         free((void *)lpWave);
  130.         dwBytesRead = 0;
  131.         return FALSE;
  132.     }
  133.     
  134.     CloseHandle(hFile);
  135.     return TRUE;
  136. }
  137.  
  138.